//获取后台的用户值，展示在页面
$(function () {
    var users = JSON.parse(localStorage.getItem("users"));

    var indexx = localStorage.getItem("index");
    if (indexx != 0) {
        for (var i in users) {
            if (indexx == i) {
                var user = users.concat().slice(i, i + 1);//剪切
                // console.log(user);
                var string = template("demo", { users: user })//在{模板里的名字：需要的数据}
                document.getElementById('tb').innerHTML = string;
            }
            $("#Btns").css("display", "none");
            $(".layui-btn-danger").css("display", "none");
        }
    } else {
        console.log(users);
        var string = template("demo", { users: users })
        document.getElementById('tb').innerHTML = string;
    }

});

//新增
function doAdd() {
    //新增用户，没校验名字，密码长度，手机号,未校对admin
    //可存储进后台
    var index = localStorage.getItem("index");
    layui.use("layer", function () {
        var layer = layui.layer;
        newOne = layer.open({
            type: 1,
            content: $('#msg'),
            area: ['500px', '300px'],
            btn: ['新增', '关闭'],
            yes: function (index, layero) {
                //按钮【按钮一】的回调
                var users = JSON.parse(localStorage.getItem("users"));//取出的字符串转换成数组
                console.log(users)
                // var username1 = $("formXiugai input:eq(0)").val();
                // var password1 = $("formXiugai input:eq(1)").val();
                // var phone1 = $("formXiugai input:eq(2)").val();
                var obj = {};
                obj.id = users.length + 1;
                obj.username = $("#formXiugai input:eq(0)").val();
                obj.password = $("#formXiugai input:eq(1)").val();
                obj.phone = $("#formXiugai input:eq(2)").val();
                for (var i in users) {
                    if (obj.username == users[i].username) {
                        alert("用户已存在")
                    } else {
                        users.push(obj);
                        users = JSON.stringify(users);
                        localStorage.setItem('users', users);
                        layer.close(newOne);
                        location.reload();
                    }
                }
            }
        });
    })
}

//删除全部，(后台数据一并删除)
function doDelAll() {
    $("#tb").children().remove();
    var users = JSON.parse(localStorage.getItem("users"));//取出的字符串转换成数组
    users.length = 1;
    users = JSON.stringify(users);
    localStorage.setItem('users', users);
    location.reload();
}

//删除单行,(后台数据一并删除)
function doDel(obj) {
    var users = JSON.parse(localStorage.getItem("users"));//取出的字符串转换成数组
    var tds = $(obj).parent().parent().children();
    var index = tds.eq(0).html();//找到当前的序号

    for (var i in users) {//在对象users中遍历，i从0开始
        if (users[i].id == index) {//users的对象中的id和找到的序号进行匹配
            if (i == 0) {
                alert("不可删除");
            } else {
                users.splice(i, 1);
                $(obj).parent().parent().remove();//删除当前行
            }
        }
    }
    users = JSON.stringify(users);
    localStorage.setItem('users', users);
}
//修改,(已修改后台),未校验admin
function doChange(t) {
    layui.use('layer', function () {
        var layer = layui.layer;
        var indexx = localStorage.getItem("index");
        var tds = $(t).parent().parent().children();
        $("#formXiugai input:eq(0)").val(tds[1].innerHTML);
        $("#formXiugai input:eq(1)").val(tds[2].innerHTML);
        $("#formXiugai input:eq(2)").val(tds[3].innerHTML);
        var name = $("#formXiugai input:eq(0)").val().trim();
        
        // if (!($("#username").val("admin"))) {
            xiugai = layer.open({
                type: 1,
                content: $('#msg'),
                area: ['500px', '300px'],
                btn: ['修改', '关闭'],
                yes: function (index, layero) {
                    //按钮【按钮一】的回调
                    var obj = {};
                    var users = JSON.parse(localStorage.getItem("users"));//取出的字符串转换成数组
                    obj.username = $("#username").val();
                    obj.password = $("#password").val();
                    obj.phone = $("#phone").val();

                    for (var i in users) {
                        if (users[i].username == name) {
                            if(users[i].username=="admin"){
                                alert('不可修改')
                            }else{
                               users[i] = obj; 
                            }
                            
                        }
                    }
                    users = JSON.stringify(users);//转换成字符串
                    localStorage.setItem('users', users);
                    layer.close(xiugai);
                },
            })
        // }else{
        //     alert("不可修改");
        // }
    });
}

//查询
function doSearch() {
    flag = false;
    var users = JSON.parse(localStorage.getItem("users"));
    var inputInner = $("#search").val().trim();
    for (var i = 0; i < users.length; i++) {
        if (inputInner == users[i].username) {
            flag = true;
            $("#tb").empty();
            var obj = [users[i]];
            var string = template("demo", { users: obj })
            document.getElementById('tb').innerHTML = string;

        }
    }
    if (flag == false) {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.msg('不存在该用户');
        });
    }
}
